package com.hznu.ez4life.modules.controller;


import com.hznu.ez4life.modules.entity.User;
import com.hznu.ez4life.modules.service.LoginService;
import com.hznu.ez4life.modules.service.UserService;
import com.hznu.ez4life.myBeans.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@EnableAutoConfiguration
@RequestMapping("/login")
public class LoginController {

    @Autowired
    ConfigParameters configParameters;
    @Autowired
    LoginService loginService;
    @Autowired
    UserService userService;

//    @PostMapping("/toLogin/{code}")
//    public R toLogin(@RequestBody User user, @PathVariable String code , HttpServletRequest request)
//    {
//        try
//        {
//            LoginInfo loginInfo = loginService.onLogin(user,code,request);
//            return R.ok().put("sessionid",loginInfo.getSessionid()).put("userInfo",loginInfo.getUser());
//        }catch (Exception e)
//        {
//            e.printStackTrace();
//            return R.error();
//        }
//    }

    @GetMapping("/toLogin2")
    public R toLogin2(@RequestParam String code,HttpServletRequest httpServletRequest)
    {
        try
        {
            return R.ok().put("sessionid",loginService.create3rdSession(code,httpServletRequest));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("/getUserInfo")
    public R getUserInfo(@RequestBody User user,HttpServletRequest httpServletRequest)
    {
        try
        {
            HttpSession httpSession = httpServletRequest.getSession();
            String openid = (String) httpSession.getAttribute("openid");
            return R.ok().put("userInfo",userService.getUserInfoByOpenid(user,openid));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }


}
